# Copyright (c) 2011 Peter Shinners

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.



import os
import sys
import time
import threading
import unittest
import struct
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import minivec


class TestContext(unittest.TestCase):
    def setUp(self):
        minivec.ResetError()
        minivec.ResetEpsilon()


    def TestSetEpsilon(self):
        default = minivec.Epsilon()
        minivec.PushEpsilon(1)
        minivec.PushEpsilon(2)
        minivec.PushEpsilon(3)
        self.assertEqual(3, minivec.Epsilon())
        minivec.PopEpsilon()
        self.assertEqual(2, minivec.Epsilon())
        minivec.PopEpsilon()
        self.assertEqual(1, minivec.Epsilon())
        minivec.PopEpsilon()
        self.assertEqual(default, minivec.Epsilon())
        self.assertRaises(RuntimeError, lambda: minivec.PopEpsilon())


    def TestSetError(self):
        default = minivec.Error()
        minivec.PushError(1)
        minivec.PushError(2)
        minivec.PushError(3)
        self.assertEqual(3, minivec.Error())
        minivec.PopError()
        self.assertEqual(2, minivec.Error())
        minivec.PopError()
        self.assertEqual(1, minivec.Error())
        minivec.PopError()
        self.assertEqual(default, minivec.Error())
        self.assertRaises(RuntimeError, lambda: minivec.PopError())


    def TestPythonContext(self):
        minivec.PushEpsilon(1e-2)

        with minivec.PushEpsilon(4.0) as c:
            self.assertEqual(4.0, minivec.Epsilon())
        self.assertEqual(1e-2, minivec.Epsilon())

        with minivec.PushEpsilon(4.0):
            self.assertEqual(4.0, minivec.Epsilon())
        self.assertEqual(1e-2, minivec.Epsilon())


    def TestEpsilon(self):
        default = minivec.Epsilon()

        minivec.PushEpsilon(1e-2)
        self.assertEqual(1e-2, minivec.Epsilon())

        minivec.PushEpsilon(-1.0)
        self.assertEqual(0.0, minivec.Epsilon())

        minivec.ResetEpsilon()
        self.assertEqual(default, minivec.Epsilon())


    def TestErrorZero(self):
        minivec.PushError(0.0)
        zeroVec = minivec.Vec(0.0).normalize()
        self.assertEqual(0.0, zeroVec[0])


    def TestErrorNan(self):
        nan = struct.unpack("f", '\x00\x00\xc0\x7f')[0]
        minivec.PushError(minivec.Nan)
        nanVec = minivec.Vec(0.0).normalize()
        self.assertEqual(repr(nan), repr(nanVec[0]))


    def TestException(self):
        minivec.PushError(NotImplemented)
        self.assertRaises(ValueError, lambda: minivec.Vec(0.0).normalize())


    def TestThread(self):
        results = []

        def _ThreadCall():
            # results is from outer context
            first = minivec.Epsilon()
            minivec.PushEpsilon(12.0)
            second = minivec.Epsilon()
            results.append((first, second))

        default = minivec.Epsilon()
        minivec.PushEpsilon(8.0)
        t = threading.Thread(target=_ThreadCall)
        t.start()
        if hasattr(t, "join"):
            t.join()
        else:
            while t.isAlive():
                time.sleep(0.1)

        self.assertEqual(results, [(default, 12.0)])
        self.assertEqual(minivec.Epsilon(), 8.0)

        

if __name__ == "__main__":
    unittest.main()
